package com.my_musc_service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my_musc_service.dao.SonglistDao;
import com.my_musc_service.entity.SongListUser;
import com.my_musc_service.entity.Songlist;
import com.my_musc_service.entity.SonglistVO;
import com.my_musc_service.entity.User;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.interceptor.LoginInterceptor;
import com.my_musc_service.service.SongListCommonProp;
import com.my_musc_service.service.SonglistService;
import com.my_musc_service.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Songlist)表服务实现类
 *
 * @author makejava
 * @since 2021-06-10 20:44:50
 */
@Service("songlistService")
public class SonglistServiceImpl extends ServiceImpl<SonglistDao, Songlist> implements SonglistService, SongListCommonProp {
    @Resource
    private SonglistDao songlistDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Songlist queryById(Integer id) {
        return this.songlistDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Songlist> queryAllByLimit(int offset, int limit) {
        return this.songlistDao.queryAllByLimit(offset, limit);
    }

    /**
     * 查询多条数据
     *
     * @param songlist 实例对象
     * @return
     */
    @Override
    public List<Songlist> queryAll(Songlist songlist) {
        return this.songlistDao.queryAll(songlist);
    }

    /**
     * 新增数据
     *
     * @param songlist 实例对象
     * @return 实例对象
     */
    @Override
    public Songlist insert(Songlist songlist) {
        List<Songlist> songlists = this.songlistDao.queryAll(songlist);
        if (songlists.size() > 0) {
            throw new CustomException("当前歌单已存在此歌曲");
        }
        songlist.setCreateDate(DateUtils.getNowDate());
        this.songlistDao.insert(songlist);
        return songlist;
    }

    /**
     * 修改数据
     *
     * @param songlist 实例对象
     * @return 实例对象
     */
    @Override
    public Songlist update(Songlist songlist) {
        this.songlistDao.update(songlist);
        return this.queryById(songlist.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.songlistDao.deleteById(id) > 0;
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<SonglistVO> getSonglistPageInfo(Integer pageNo, Integer pageSize, Songlist songlist) {
        PageHelper.startPage(pageNo, pageSize);
        List<Songlist> songlists = songlistDao.queryAll(songlist);
        List<SonglistVO> songlistVOList = songlists.stream().map(songlist1 -> {
            SonglistVO songlistVO = new SonglistVO();
            BeanUtils.copyProperties(songlist1, songlistVO);
            return songlistVO;
        }).collect(Collectors.toList());

        //每一个分享歌单封面是其第一首歌的封面
//        for (SonglistVO songlistVO : songlistVOList) {
//            Integer songListId = songlistVO.getId();
//            Integer userId = songlistVO.getUserId();
//            QueryWrapper<SongListUser> songListUserQueryWrapper = new QueryWrapper<>();
//            songListUserQueryWrapper.eq("song_list_id", songListId);
//            songListUserQueryWrapper.eq("user_id", userId);
//            songListUserQueryWrapper.orderByAsc("song_index");
//            List<SongListUser> songListUserList = songListUserDao.selectList(songListUserQueryWrapper);
//            if (!CollectionUtils.isEmpty(songListUserList)){
//                SongListUser songListUser = songListUserList.get(0);
//                Integer songId = songListUser.getSongId();
//                Song song = songDao.selectOne(new QueryWrapper<Song>().eq("id", songId));
//                if (song != null && StringUtils.isNotBlank(song.getCoverUrl())) {
//                    songlistVO.setCoverUrl(song.getCoverUrl());
//                }
//            }
//        }

        setSongListCover(songlistVOList);

        PageInfo<SonglistVO> songlistPageInfo = new PageInfo<>(songlistVOList);
        return songlistPageInfo;
    }

    @Override
    public Integer deleteBySongList(Songlist songlist) {
        User userInfo = LoginInterceptor.getUserInfo();
        SongListUser songListUser = new SongListUser();
        songListUser.setSongListId(songlist.getId());
        songListUser.setSongListId(userInfo.getId());
        List<SongListUser> songListUserList = songListUserDao.queryAll(songListUser);
        if (!CollectionUtils.isEmpty(songListUserList)) {
            List<Integer> ids = songListUserList.stream().map(songListUser1 -> songListUser1.getId()).collect(Collectors.toList());
            Integer rows = songListUserDao.deleteSongListUser(ids);
        }
        return songlistDao.deleteBySongList(songlist);
    }
}
